package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private String id;//存放输入的学号或教师号或管理员号
    private String pwd;//存放输入的密码
    private String name;//存放要传输到下一个activity的姓名
    private String character;//存放身份，学生或教师或管理员
    private String table;//用于存放要查找的表的名字
    private int radioed;//获取radio的输入
    private MyDataBaseHelper helper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);//设置布局文件

        //初始化控件
        EditText login_id = (EditText) findViewById(R.id.login_id);
        EditText login_pwd = (EditText) findViewById(R.id.login_pwd);
        RadioGroup identity = (RadioGroup) findViewById(R.id.radioGroup);
        Button login_button = (Button) findViewById(R.id.button_login);
        Button register_button = findViewById(R.id.button_register);
        //初始化数据库
        try {
            helper = new MyDataBaseHelper(LoginActivity.this,"test72.dp",null,32);
            Log.d("LoginActivity","数据库打开成功");
        }catch (Exception e){
            e.printStackTrace();
            Log.d("LoginActivity","数据库助手创建失败");
            Toast.makeText(this,"数据库创建错误",Toast.LENGTH_SHORT).show();
        }

        //注册按钮，跳转到注册页面
        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        //按下登录按钮后，先检查输入是否为空。若非空则将传入的
        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(login_id.getText().toString().trim().equals("") || login_pwd.getText().toString().trim().equals("") ){
                    Toast.makeText(LoginActivity.this,"输入不可为空",Toast.LENGTH_SHORT).show();
                }else{
                    //获取输入：学号id，密码pwd，身份character
                    id = login_id.getText().toString().trim();
                    pwd = login_pwd.getText().toString().trim();
                    radioed = identity.getCheckedRadioButtonId();
                    if(radioed == R.id.radioButton_stu){
                        character = "学生";
                        table = "stu";
                    } else if (radioed == R.id.radioButton_tea) {
                        character = "教师";
                        table = "tea";
                    }else if(radioed == R.id.radioButton_admin){
                        character = "管理员";
                        table = "admin";
                    }else{
                        character = "入侵者";
                        table = "nope";
                    }
                    try{
                        //数据库查找操作，根据输入的id和pwd查找对应项
                        SQLiteDatabase db = helper.getWritableDatabase();
                        Cursor cursor = db.query(table,null,"id=? and pwd=?",new String[]{id,pwd},null,null,null);
                        if(cursor.moveToFirst()){
                            name = cursor.getString(cursor.getColumnIndexOrThrow("name"));//获取数据库中的姓名一栏

                            // 输出调试信息，用于改错
                            Log.d("LoginActivity","id"+id);
                            Log.d("LoginActivity","name:"+name);
                            Log.d("LoginActivity","pwd:"+pwd);

                            //关闭cursor和db
                            cursor.close();
                            db.close();

                            //用Intent启动下一个活动,应当跳转到登录后的界面，SecondActivity只是占位用
                            Intent intent;
                            if(character.equals("学生")){
                                intent = new Intent(LoginActivity.this,StudentActivity.class);
                            } else if (character.equals("教师")) {
                                intent = new Intent(LoginActivity.this,StudentActivity.class);
                            } else if (character.equals("管理员")) {

                                //把这里跳转的Activity换成你的
                                intent = new Intent(LoginActivity.this,AdminActivity.class);

                            }else{
                                intent = new Intent(LoginActivity.this, InvaderActivity.class);
                            }

                            //清空所有输入框，将radio复位
                            login_id.setText("");
                            login_pwd.setText("");
                            identity.check(R.id.radioButton_stu);

                            //组装存放用户信息的info对象
                            user_info info = new user_info(id,pwd,name,character);
                            intent.putExtra("user_info",info);
                            startActivity(intent);
                        }else{
                            Toast.makeText(LoginActivity.this,"登录失败，请检查输入",Toast.LENGTH_SHORT).show();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        Toast.makeText(LoginActivity.this,"数据库查询出错",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}